package emulator.hardware.nmos6502.commands;

import emulator.EmulatorException;
import emulator.hardware.HwByte;
import emulator.hardware.nmos6502.Command;
import emulator.hardware.nmos6502.CommandSet;
import emulator.hardware.nmos6502.Cpu6502;
import emulator.hardware.nmos6502.Flags6502;
import emulator.hardware.nmos6502.Operand;
import emulator.hardware.nmos6502.operands.*;

/** ORA (bitwise OR with Accumulator)
 *  
 *  Affects Flags: S Z
 *  
 *  MODE           SYNTAX       HEX LEN TIM
 *  Immediate     ORA #$44      $09  2   2
 *  Zero Page     ORA $44       $05  2   2
 *  Zero Page,X   ORA $44,X     $15  2   3
 *  Absolute      ORA $4400     $0D  3   4
 *  Absolute,X    ORA $4400,X   $1D  3   4+
 *  Absolute,Y    ORA $4400,Y   $19  3   4+
 *  Indirect,X    ORA ($44,X)   $01  2   6
 *  Indirect,Y    ORA ($44),Y   $11  2   5+
 *  
 *  + add 1 cycle if page boundary crossed
 */
public class ORA implements Command {

	@Override
	public void execute(Cpu6502 cpu, Operand operand) throws EmulatorException {
		HwByte value = cpu.getAccu();
		value.or(operand.getByte());
		cpu.setAccu(value);
		Flags6502 flags = cpu.getFlags();
		flags.setNZFromValue(value);
		cpu.setFlags(flags);
	}

	@Override
	public String getName() {
		return "ORA";
	}

	@Override
	public void register(CommandSet cset) {
		cset.defineCommand(0x09, this, new Immediate());
		cset.defineCommand(0x05, this, new ZeroPage());
		cset.defineCommand(0x15, this, new ZeroPageX());
		cset.defineCommand(0x0D, this, new Absolute());
		cset.defineCommand(0x1D, this, new AbsoluteX());
		cset.defineCommand(0x19, this, new AbsoluteY());
		cset.defineCommand(0x01, this, new IndirectX());
		cset.defineCommand(0x11, this, new IndirectY());
	}

}
